/**
 * 替换长文本
 * @param text
 *
 * @return {String}
 */
const text = (text) => {
    String.prototype.replaceAll = function (a, b) {
        return this.split(a).join(b);
    };
    return text.replaceAll("%7e", "~").replaceAll("%8a", "Š").replaceAll("%8b", "‹")
        .replaceAll("%88", "ˆ").replaceAll("%2f", "/").replaceAll("%5c", "\\").replaceAll("%5e", "^")
        .replaceAll("%60", "`").replaceAll("%80", "€").replaceAll("%83", "ƒ").replaceAll("%84", "„").replaceAll("%85", "…")
        .replaceAll("%87", "‡").replaceAll("%89", "‰").replaceAll("%8a", "Š").replaceAll("%8c", "Œ").replaceAll("%8e", "Ž").replace(/&gt;/g, '>').replace(/&lt;/g, '<')
        .replace(/&nbsp;/g, " ").replace(/&quot;/g, "\"")
        .replace(/&#39;/g, "\'").replace(/\\\\/g, "\\").replaceAll("\v", "").replaceAll("\t", "").replaceAll("<br/>", "\n")
        .replaceAll("</p><p>", "\n").replaceAll("<p>", "").replaceAll("</p>", "").replace(/<p[^>]*>/g, "")
        .replaceAll("<span>", "").replaceAll("</span>", "").replace(/<span[^>]*>/g, "")
        .replaceAll("<strong>", "").replaceAll("</strong>", "").replace(/<strong[^>]*>/g, "")
        .replaceAll("<em>", "").replaceAll("</em>", "").replace(/<em[^>]*>/g, "")
        .replaceAll(/<img[^>]*>/g, "").replaceAll("%7e", "~").replaceAll("%8a", "Š").replaceAll("%8b", "‹")
        .replaceAll("%88", "ˆ");
};

/*
** 文本转标签
*/
const textToHTML = text => {
    return text.replace(/\n/g, '<br/>')
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>');
};

/**
 * 替换缺省值
 * @param LOGINITEM 用户信息
 * @param con 需替换字符串
 * @param code 替换用格式（如',"等）
 *
 * @return {String}
 */
const def = (LOGINITEM, con, code) => {
    /* 不为空 */
    const notNull = (v) => {
        return v || '';
    };

    if (!con) {
        return "";
    } else {
        /* 当code存在时，替换后值需用code表示的符号包裹 */
        /* 用户信息 */
        if (con.indexOf('LOGINUSER.userName') > -1) {
            con = con.replace(/\LOGINUSER.userName/g, code ? code + notNull(LOGINITEM.userName) + code : notNull(LOGINITEM.userName));
        }
        if (con.indexOf('${loginUser.userName}') > -1) {
            con = con.replace(/\${loginUser.userName}/g, code ? code + LOGINITEM.userName + code : notNull(LOGINITEM.userName));
        }
        if (con.indexOf('LOGINUSER.userAccout') > -1) {
            con = con.replace(/\LOGINUSER.userAccout/g, code ? code + LOGINITEM.userAccout + code : notNull(LOGINITEM.userAccout));
        }
        if (con.indexOf('${loginUser.userAccout}') > -1) {
            con = con.replace(/\${loginUser.userAccout}/g, code ? code + LOGINITEM.userAccout + code : notNull(LOGINITEM.userAccout));
        }
        if (con.indexOf('LOGINUSER.userType') > -1) {
            con = con.replace(/\LOGINUSER.userType/g, code ? code + LOGINITEM.userType + code : notNull(LOGINITEM.userType));
        }
        if (con.indexOf('${loginUser.userType}') > -1) {
            con = con.replace(/\${loginUser.userType}/g, code ? code + LOGINITEM.userType + code : notNull(LOGINITEM.userType));
        }
        if (con.indexOf('LOGINUSER.userId') > -1) {
            con = con.replace(/\LOGINUSER.userId/g, code ? code + LOGINITEM.id + code : notNull(LOGINITEM.id));
        }
        if (con.indexOf('${loginUser.id}') > -1) {
            con = con.replace(/\${loginUser.id}/g, code ? code + LOGINITEM.id + code : notNull(LOGINITEM.id));
        }
        if (con.indexOf('${loginUser.linkId}') > -1) {
            con = con.replace(/\${loginUser.linkId}/g, code ? code + LOGINITEM.linkId + code : notNull(LOGINITEM.linkId));
        }
        if (con.indexOf('LOGINUSER.linkId') > -1) {
            con = con.replace(/\LOGINUSER.linkId/g, code ? code + LOGINITEM.linkId + code : notNull(LOGINITEM.linkId));
        }
        //组织信息
        if (con.indexOf('LOGINUSER.mainOrgName') > -1) {
            con = con.replace(/\LOGINUSER.mainOrgName/g, code ? code + LOGINITEM.mainOrgName + code : notNull(LOGINITEM.mainOrgName));
        }
        if (con.indexOf('${loginUser.mainOrgName}') > -1) {
            con = con.replace(/\${loginUser.mainOrgName}/g, code ? code + LOGINITEM.mainOrgName + code : notNull(LOGINITEM.mainOrgName));
        }
        if (con.indexOf('LOGINUSER.mainOrgId') > -1) {
            con = con.replace(/\LOGINUSER.mainOrgId/g, code ? code + LOGINITEM.mainOrgId + code : notNull(LOGINITEM.mainOrgId));
        }
        if (con.indexOf('${loginUser.mainOrgId}') > -1) {
            con = con.replace(/\${loginUser.mainOrgId}/g, code ? code + LOGINITEM.mainOrgId + code : notNull(LOGINITEM.mainOrgId));
        }
        if (con.indexOf('LOGINUSER.orgPosUserIds') > -1) {
            con = con.replace(/\LOGINUSER.orgPosUserIds/g, code ? code + LOGINITEM.orgPosUserIds + code : notNull(LOGINITEM.orgPosUserIds));
        }
        if (con.indexOf('${loginUser.orgPosUserIds}') > -1) {
            con = con.replace(/\${loginUser.orgPosUserIds}/g, code ? code + LOGINITEM.orgPosUserIds + code : notNull(LOGINITEM.orgPosUserIds));
        }

        /* 部门信息 */
        if (con.indexOf('${loginDept.id}') > -1) {
            con = con.replace(/\${loginDept.id}/g, code ? code + LOGINITEM.id + code : notNull(LOGINITEM.id));
        }
        if (con.indexOf('LOGINDEPT.id') > -1) {
            con = con.replace(/\LOGINDEPT.id/g, code ? code + LOGINITEM.id + code : notNull(LOGINITEM.id));
        }
        if (con.indexOf('${loginDept.orgName}') > -1) {
            con = con.replace(/\${loginDept.orgName}/g, code ? code + LOGINITEM.orgName + code : notNull(LOGINITEM.orgName));
        }
        if (con.indexOf('LOGINDEPT.orgName') > -1) {
            con = con.replace(/\LOGINDEPT.orgName/g, code ? code + LOGINITEM.orgName + code : notNull(LOGINITEM.orgName));
        }

        /* 用户行政区划信息 */
        if (con.indexOf('LOGINDEPT.areaCode') > -1) {
            con = con.replace(/\LOGINDEPT.areaCode/g, code ? code + LOGINITEM.areaCode + code : notNull(LOGINITEM.areaCode));
        }

        /* 省 */
        if (con.indexOf('${loginDept.provinceName}') > -1) {
            con = con.replace(/\${loginDept.provinceName}/g, code ? code + LOGINITEM.provinceName + code : notNull(LOGINITEM.provinceName));
        }
        if (con.indexOf('LOGINDEPT.provinceName') > -1) {
            con = con.replace(/\LOGINDEPT.provinceName/g, code ? code + LOGINITEM.provinceName + code : notNull(LOGINITEM.provinceName));
        }
        if (con.indexOf('${loginDept.province}') > -1) {
            con = con.replace(/\${loginDept.province}/g, code ? code + LOGINITEM.province + code : notNull(LOGINITEM.province));
        }
        if (con.indexOf('LOGINDEPT.province') > -1) {
            con = con.replace(/\LOGINDEPT.province/g, code ? code + LOGINITEM.province + code : notNull(LOGINITEM.province));
        }
        /* 市 */
        if (con.indexOf('${loginDept.cityName}') > -1) {
            con = con.replace(/\${loginDept.cityName}/g, code ? code + LOGINITEM.cityName + code : notNull(LOGINITEM.cityName));
        }
        if (con.indexOf('LOGINDEPT.cityName') > -1) {
            con = con.replace(/\LOGINDEPT.cityName/g, code ? code + LOGINITEM.cityName + code : notNull(LOGINITEM.cityName));
        }
        if (con.indexOf('${loginDept.city}') > -1) {
            con = con.replace(/\${loginDept.city}/g, code ? code + LOGINITEM.city + code : notNull(LOGINITEM.city));
        }
        if (con.indexOf('LOGINDEPT.city') > -1) {
            con = con.replace(/\LOGINDEPT.city/g, code ? code + LOGINITEM.city + code : notNull(LOGINITEM.city));
        }

        /* 区县 */
        if (con.indexOf('${loginDept.areaName}') > -1) {
            con = con.replace(/\${loginDept.areaName}/g, code ? code + LOGINITEM.areaName + code : notNull(LOGINITEM.areaName));
        }
        if (con.indexOf('LOGINDEPT.areaName') > -1) {
            con = con.replace(/\LOGINDEPT.areaName/g, code ? code + LOGINITEM.areaName + code : notNull(LOGINITEM.areaName));
        }
        if (con.indexOf('${loginDept.area}') > -1) {
            con = con.replace(/\${loginDept.area}/g, code ? code + LOGINITEM.area + code : notNull(LOGINITEM.area));
        }
        if (con.indexOf('LOGINDEPT.area') > -1) {
            con = con.replace(/\LOGINDEPT.area/g, code ? code + LOGINITEM.area + code : notNull(LOGINITEM.area));
        }

        /* 街道 */
        if (con.indexOf('${loginDept.streetName}') > -1) {
            con = con.replace(/\${loginDept.streetName}/g, code ? code + LOGINITEM.streetName + code : notNull(LOGINITEM.streetName));
        }
        if (con.indexOf('LOGINDEPT.streetName') > -1) {
            con = con.replace(/\LOGINDEPT.streetName/g, code ? code + LOGINITEM.streetName + code : notNull(LOGINITEM.streetName));
        }
        if (con.indexOf('${loginDept.street}') > -1) {
            con = con.replace(/\${loginDept.street}/g, code ? code + LOGINITEM.street + code : notNull(LOGINITEM.street));
        }
        if (con.indexOf('LOGINDEPT.street') > -1) {
            con = con.replace(/\LOGINDEPT.street/g, code ? code + LOGINITEM.street + code : notNull(LOGINITEM.street));
        }
        if (con.indexOf('SYSTIME') > -1) {
            this.axios.post("commonmethodController/getNowTime").then(res => {
                let nyr = res.split(" ")[0];
                let sfm = res.split(" ")[1];
                let year = nyr.split("-")[0];
                let mon = nyr.split("-")[1];
                let day = nyr.split("-")[2];
                let hour = sfm.split(":")[0];
                let min = sfm.split(":")[1];
                let sec = sfm.split(":")[2];
                const SYSTIME  = {
                    "time": res,//年-月-日 时：分：秒
                    "yearMoNday": nyr,//年-月-日
                    "hourMinSec": sfm,//时:分:秒
                    "yearMon": year + "-" + mon,//年-月
                    "onlyYear": year,//年
                    "onlyMon": mon,//月
                    "onlyDay": day,//日
                    "hourMin": hour + ":" + min,//时:分
                    "onlyHour": hour,//时
                    "onlyMin": min,//分
                    "onlySec": sec//秒
                };
                 /* 系统时间信息 */
                if (con.indexOf('SYSTIME.time') > -1) {
                        con = con.replace(/\SYSTIME.time/g, code ? code + SYSTIME.time + code : notNull(SYSTIME.time));
                }
                if (con.indexOf('SYSTIME.yearMoNday') > -1) {
                    con = con.replace(/\SYSTIME.yearMoNday/g, code ? code + SYSTIME.yearMoNday + code : notNull(SYSTIME.yearMoNday));
                }
                if (con.indexOf('SYSTIME.hourMinSec') > -1) {
                    con = con.replace(/\SYSTIME.hourMinSec/g, code ? code + SYSTIME.hourMinSec + code : notNull(SYSTIME.hourMinSec));
                }
                if (con.indexOf('SYSTIME.yearMon') > -1) {
                    con = con.replace(/\SYSTIME.yearMon/g, code ? code + SYSTIME.yearMon + code : notNull(SYSTIME.yearMon));
                }
                if (con.indexOf('SYSTIME.onlyYear') > -1) {
                    con = con.replace(/\SYSTIME.onlyYear/g, code ? code + SYSTIME.onlyYear + code : notNull(SYSTIME.onlyYear));
                }
                if (con.indexOf('SYSTIME.onlyMon') > -1) {
                    con = con.replace(/\SYSTIME.onlyMon/g, code ? code + SYSTIME.onlyMon + code : notNull(SYSTIME.onlyMon));
                }
                if (con.indexOf('SYSTIME.onlyDay') > -1) {
                    con = con.replace(/\SYSTIME.onlyDay/g, code ? code + SYSTIME.onlyDay + code : notNull(SYSTIME.onlyDay));
                }
                if (con.indexOf('SYSTIME.onlyHour') > -1) {
                    con = con.replace(/\SYSTIME.onlyHour/g, code ? code + SYSTIME.onlyHour + code : notNull(SYSTIME.onlyHour));
                }
                if (con.indexOf('SYSTIME.onlyMin') > -1) {
                    con = con.replace(/\SYSTIME.onlyMin/g, code ? code + SYSTIME.onlyMin + code : notNull(SYSTIME.onlyMin));
                }
                if (con.indexOf('SYSTIME.onlySec') > -1) {
                    con = con.replace(/\SYSTIME.onlySec/g, code ? code + SYSTIME.onlySec + code : notNull(SYSTIME.onlySec));
                }
                return con;
            });
        }else{
            return con;
        }

    }
};

export default {text, def, textToHTML};